package org.shopen.ai.puremvc.model.domain.cities
{
	import mx.collections.ArrayCollection;
	
	import org.shopen.ai.puremvc.model.IRavisSerializable;
	import org.shopen.ai.puremvc.model.IReadable;
	import org.shopen.ai.puremvc.model.ISerializable;
	import org.shopen.ai.puremvc.model.ITitleable;
	import org.shopen.ai.puremvc.model.utils.Convert;

	public class Country implements ISerializable, IRavisSerializable, IReadable, ITitleable
	{
		[Bindable]
		private var _title : String = "";
		[Bindable]
		public function get title() : String
		{
			return _title;
		}
		public function set title(value : String) : void
		{
			_title = value;
		}
		
		[Bindable]
		private var _cities : ArrayCollection = new ArrayCollection();
		[Bindable]
		public function get cities() : ArrayCollection
		{
			return _cities;
		}
		public function set cities(value : ArrayCollection) : void
		{
			_cities = value;
		}
		
		[Bindable]
		private var _paths : ArrayCollection = new ArrayCollection();
		[Bindable]
		public function get paths() : ArrayCollection
		{
			return _paths;
		}
		public function set paths(value : ArrayCollection) : void
		{
			_paths = value;
		}
		
		public function Country(node : XML = null)
		{
			if (node != null)
			{
				deserialize(node);
			}
		}
		
		public function deserialize(node : XML) : void
		{
			if ("@title" in node)
			{
				this.title = Convert.toString(node.@title);
			}
			
			this.cities.removeAll();
			for each (var cityNode : XML in node.cities..city)
			{
				var city : City = new City(cityNode);
				this.cities.addItem(city);
			}
			
			this.paths.removeAll();
			for each (var pathNode : XML in node.paths..path)
			{
				var path : Path = new Path(pathNode);
				this.paths.addItem(path);
			}
		}
		
		public function serialize() : XML
		{
			// TODO: not yet implemented
			return new XML();
		}
		
		public function toRavisXml() : XML
		{
			var graph : XML = new XML(<Graph> </Graph>);
			for each (var city : City in this.cities)
			{
				var cityNode : XML = city.toRavisXml();
				graph.appendChild(cityNode);
			}
			for each (var path : Path in this.paths)
			{
				var pathNode : XML = path.toRavisXml();
				graph.appendChild(pathNode);
			}
			return graph;
		}
		
		public function toString():String
		{
			return this.title;
		}
	}
}